package com.ccys.cms.service.consultation.impl;


import com.ccys.cms.service.consultation.BookingQueueWebService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.consultation.BookingQueueDTO;
import com.ccys.db.entity.TblBookingQueue;
import com.ccys.db.mapper.TblBookingQueueMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.consultation.BookingAppointmentWebVO;
import com.ccys.db.vo.consultation.BookingQueueWebVO;

import com.ccys.db.vo.consultation.CountBookingNumWebVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 问诊队列，医生结束问诊后真删管理
 */
@Service
@Slf4j
public class BookingQueueWebServiceImpl extends BaseServiceImpl<TblBookingQueue> implements BookingQueueWebService {

    @Autowired(required = false)
    private TblBookingQueueMapper bookingQueueMapper;



    @Override
    @Transactional
    public ResponseResult change(TblBookingQueue entity) {
        Integer queueNo1 = entity.getQueueNo();
        if(StringUtil.isEmpty(entity.getId())) {
            return ResponseResult.error("id is not null");
        }
        if(queueNo1 == null){
            return ResponseResult.error("queueNo parameter Exception");
        }
        TblBookingQueue entityById = this.findEntityById(entity.getId());
        if(entityById == null) {
            return ResponseResult.error("对象不存在");
        }
        Integer queueNo = entityById.getQueueNo();
        if(queueNo == null){
            return ResponseResult.error("Queue init Exception");
        }
        if(queueNo1 > queueNo) {
            //向后移动对列
            this.bookingQueueMapper.updateBackwardMoveQueue(queueNo,queueNo1);
        }else{
            //向前移动对列
            this.bookingQueueMapper.updateForwardMoveQueue(queueNo,queueNo1);
        }
        TblBookingQueue queue = new TblBookingQueue();
        queue.setId(entityById.getId());
        queue.setQueueNo(entity.getQueueNo());
        this.updateEntityById(queue);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(TblBookingQueue dto) {
        this.deleteById(dto.getId());
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listObjectsCms(BookingQueueDTO dto) {
        PageDto.startPage();
        List<BookingQueueWebVO> list = this.bookingQueueMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }

    @Override
    public ResponseResult countBookingNum(BookingQueueDTO dto) {
        PageDto.startPage();
        List<CountBookingNumWebVO> list = this.bookingQueueMapper.countBookingNum(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


}